package org.huawei260.TiKu.ODsuanfa;

import java.util.LinkedList;
import java.util.Scanner;
public class D_2024D_3398__DP_园区参观路径 {

//	public class Main {
		static int n;
		static int m;
		static int[][] matrix;
		static int[][] offsets = {{1, 0}, {0, 1}};

		static int ans = 0;

		public static void main(String[] args) {
			Scanner sc = new Scanner(System.in);

			n = sc.nextInt(); // 长 -> 行数
			m = sc.nextInt(); // 宽 -> 列数

			matrix = new int[n][m]; // 地图矩阵
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					matrix[i][j] = sc.nextInt();
				}
			}

			dfs();

			System.out.println(ans);
		}

		public static void dfs() {
			LinkedList<Integer> stack = new LinkedList<>();

			if (matrix[0][0] == 0) {
				stack.addLast(0);
			}

			while (stack.size() > 0) {
				int pos = stack.removeLast();

				int x = pos / m;
				int y = pos % m;

				if (x == n - 1 && y == m - 1) {
					ans++;
					continue;
				}

				for (int[] offset : offsets) {
					int newX = x + offset[0];
					int newY = y + offset[1];

					if (newX >= 0 && newX < n && newY >= 0 && newY < m && matrix[newX][newY] == 0) {
						stack.addLast(newX * m + newY);
					}
				}
			}
		}
	}